﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.SysInterface;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.DAL;

namespace RoadTeam.BsSystem.FM
{
    public class FmMeterDegree_Sys :  IMeterDegree
    {
        public DataTable GetMeterDegree(String OrganID, int PageSize, int CurrentPage, int AllCount)
        {
            string Str = @"select
DegreeID,
TbRmUnit.UnitName,
TbRmUnit.UnitCode,
TbFmMeter.MeterName,
TbFmMeter.MeterCode,
isnull(PreDegree,0) as PreDegree,
isnull(CurDegree,0) as CurDegree,
isnull(Degree,0) as Degree,
Times,
Capacity
from TbFmMeterDegree 
Inner join TbFmMeter 
on TbFmMeter.MeterID=TbFmMeterDegree.MeterID
INNER JOIN TbRmUnit 
on TbRmUnit.UnitID=TbFmMeter.UnitID
WHERE TbFmMeterDegree.OrganID=" + OrganID + @" 
AND TbFmMeterDegree.EndDate='<当前抄表期间>'
ORDER BY TbFmMeterDegree.DegreeID
";
            String StrSql = SQLPage.MakeSQLStringByPage(Str, String.Empty, PageSize, CurrentPage, AllCount);
            DataTable dt = DbFactory.CreateInstance().CreateDb().
         ExecuteStrQuery(StrSql, CommandType.Text, GlobalConfig.RtConnectionString);
            return dt;
        }


        public int GetMeterDegreeCount(String OrganID)
        {
            string Str = @"select Count(*)
from TbFmMeterDegree 
Inner join TbFmMeter 
on TbFmMeter.MeterID=TbFmMeterDegree.MeterID
INNER JOIN TbRmUnit 
on TbRmUnit.UnitID=TbFmMeter.UnitID
WHERE TbFmMeterDegree.OrganID=" + OrganID + @"
AND TbFmMeterDegree.EndDate='<当前抄表期间>'
";
            DataTable dt = DbFactory.CreateInstance().CreateDb().
         ExecuteStrQuery(Str, CommandType.Text, GlobalConfig.RtConnectionString);

            if (dt.Rows.Count!=0)
            {
                return Convert.ToInt32(dt.Rows[0][0]);
            }
            return 0;

        }


        public DataTable GetMeterDegree(String StrDegreeID)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = @" DegreeID IN (" + StrDegreeID + ") ";
            where.SelectColumn = @" DegreeID,PreDegree,CurDegree,Degree ";
            return TbFmMeterDegree.Select(where, GlobalConfig.RtConnectionString);
        }

        public bool UpdateMeterDegree(DataTable dt)
        {
           return TbFmMeterDegree.Update(dt, GlobalConfig.RtConnectionString);
        }


    }
}
